home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / MV.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  3KB  |  131 lines

  1. /* +++Date last modified: 28-Sep-1996 */
  2.  
  3. /*
  4. ** mv.c -- move or rename files or directories  
  5. ** updated for multiple files, 5 jul 92, rlm    
  6. ** placed in the public domain via C_ECHO by the author, Ray McVay
  7. **
  8. ** modified by Bob Stout, 28 Mar 93
  9. ** modified by Bob Stout,  4 Jun 93
  10. **
  11. ** uses file_copy from SNIPPETS file WB_FCOPY.C
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <dos.h>
  18. #include "dirport.h"
  19. #include "snipfile.h"
  20. #include "dosfiles.h"
  21. #include "unistd.h"
  22.  
  23. /*
  24. **  Tell 'em they messed up
  25. */
  26.  
  27. void help(char *s)
  28. {
  29.       puts("usage: mv <oldname [...]> <newname|newdir>");
  30.       printf("error: %s\n", s);
  31. }
  32.  
  33. /*
  34. **  Simple directory test
  35. */
  36.  
  37. isdir(char *path)
  38. {
  39.       DOSFileData f;
  40.  
  41.       /* "Raw" drive specs are always directories     */
  42.  
  43.       if (':' == path[1] && '\0' == path[2])
  44.             return 1;
  45.  
  46.       return (Success_ == FIND_FIRST(path, _A_SUBDIR, &f) &&
  47.             (ff_attr(&f) & _A_SUBDIR));
  48. }
  49.  
  50. /*
  51. **  Use rename or copy and delete
  52. */
  53.  
  54. int mv(char *src, char *dest)
  55. {
  56.       int errcount = 0;
  57.       char buf[FILENAME_MAX];
  58.       const char *generr = "ERROR: mv - couldn't %s %s %s\n";
  59.  
  60.       if (':' == dest[1] && *dest != *getcwd(buf, FILENAME_MAX))
  61.       {
  62.             if (file_copy(src, dest))
  63.             {
  64.                   printf(generr, "move", src, dest);
  65.                   ++errcount;
  66.             }
  67.             else if (remove(src))
  68.             {
  69.                   printf(generr, "delete", src, "");
  70.                   ++errcount;
  71.             }
  72.       }
  73.       else
  74.       {
  75.             if (rename(src, dest))
  76.             {
  77.                   printf(generr, "rename", src, dest);
  78.                   ++errcount;
  79.             }
  80.       }
  81.       return errcount;
  82. }
  83.  
  84. /*
  85. **  Enter here
  86. */
  87.  
  88. int main(int argc, char **argv)
  89. {
  90.       int src, errcount = 0;
  91.       char target[FILENAME_MAX];
  92.  
  93.       puts("mv 1.3 (4 jun 93) - Ray L. McVay/Bob Stout");
  94.       if (argc < 3)
  95.             help("Not enough parameters");
  96.  
  97.       /*
  98.       **  Handle cases where target is a directory
  99.       */
  100.  
  101.       else if (isdir(argv[argc -1]))
  102.       {
  103.             for (src = 1; src < argc - 1; src++)
  104.             {
  105.                   char termch;
  106.  
  107.                   strcpy(target, argv[argc - 1]);
  108.                   termch = target[strlen(target) - 1];
  109.                   if ('\\' != termch && ':' != termch)
  110.                         strcat(target, "\\");
  111.  
  112.                   if (strrchr(argv[src], '\\'))
  113.                         strcat(target, strrchr(argv[src], '\\') + 1);
  114.                   else if (argv[src][1] == ':')
  115.                         strcat(target, argv[src] + 2);
  116.                   else  strcat(target, argv[src]);
  117.  
  118.                   errcount += mv(argv[src], target);
  119.             }
  120.       }
  121.  
  122.       /*
  123.       **  Nothing left except 2 explicit file names
  124.       */
  125.  
  126.       else if (argc == 3)
  127.             errcount += mv(argv[1], argv[2]);
  128.  
  129.       return errcount;
  130. }
  131.